package com.liangzong.betspider.executor;

import com.liangzong.betspider.entity.TeamInfo;
import com.liangzong.betspider.enums.CommandEnum;
import com.liangzong.betspider.command.AddTeamInfoCMD;
import com.liangzong.betspider.service.TeamInfoService;
import com.liangzong.betspider.service.TeamPointService;
import com.liangzong.betspider.service.TeamScoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Component
public class AddTeamExecutor implements Executor<AddTeamInfoCMD> {

    @Autowired
    TeamInfoService teamInfoService;

    @Autowired
    TeamPointService teamPointService;

    @Autowired
    TeamScoreService teamScoreService;

    @Override
    public boolean support(CommandEnum commandEnum) {
        return commandEnum == CommandEnum.ADD;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void process(AddTeamInfoCMD command) {
        log.info(command.getTeamInfo().toString());
        TeamInfo teamInfo = command.getTeamInfo();
        if(teamInfo.getHomeTeamName().indexOf("Esports")!=-1){
            log.info("抛弃电竞{} vs {}",teamInfo.getHomeTeamName(),teamInfo.getAwayTeamName());
            return;
        }
        teamInfoService.addOrUpdate(teamInfo);
        teamPointService.addOrUpdate(teamInfo.getId(), teamInfo.getHomeTeamPointList(), teamInfo.getAwayTeamPointList());
        teamScoreService.addOrUpdate(teamInfo.getId(), teamInfo.getHomeTeamScore(), teamInfo.getAwayTeamScore());
    }
}
